<?php

namespace app\store\controller\ditui;

use app\store\controller\Controller;
use app\store\model\Store as StoreModel;
use think\Db;

class Xiaoke extends Controller
{
    public function index()
    {

    }

    /** 获取每个学员的所有消课情况 */
    public function getPerXK(){
        $paramArr = $this->request->param();
        $dl_id = isset($paramArr['dl_id']) ? $paramArr['dl_id'] : 0;

        $list = $this->getPerXueKC($dl_id);

        $back['list'] = $list;
        return $this->renderSuccess('数据请求成功', '', $back);
    }

    /** 为该学员添加课程 */
    public function addKe(){
        $paramArr = $this->request->param();
        $dl_id = isset($paramArr['dl_id']) ? $paramArr['dl_id'] : 0;
        $keId = isset($paramArr['keId']) ? $paramArr['keId'] : 0;
        $opm = isset($paramArr['opm']) ? $paramArr['opm'] : 'add';
        if($dl_id>0 && $keId>0){
            $keObj = $this->getKeZd($keId);
            $keTxt = explode('#', $keObj);
            if($opm=='add'){
                $perXKobj = Db::name('ditui_user_xiaoke')->where(['ux_dl_id'=>$dl_id, 'ux_ket_id'=>$keId, 'ux_shopid'=>$this->ad_shopid])->find();
                if(!$perXKobj) {
                    $inArr['ux_dl_id'] = $dl_id;
                    $inArr['ux_ket_id'] = $keId;
                    $inArr['ux_shopid'] = $this->ad_shopid;
                    $inArr['wxapp_id'] = $this->wxapp_id;
                    $inArr['ux_addtime'] = date('Y-m-d H:i:s', time());

                    $arr1 = array();
                    for($i=0; $i<$keTxt[1]; $i++){
                        //k_type:0,无，10：正常消课，20：确课，30：请假，40：补课，50：自定义消课，可以消10节以上的课
                        $cellArr = array('k_dateStr'=>'日 期', 'k_bukeDate'=>'', 'k_type'=>'0', 'k_use'=>'0', 'k_typestr'=>'', 'k_note'=>'', 'addtime'=>'');
                        $arr1[] = $cellArr;
                    }
                    $inArr['ux_xiaoke_jd'] = "0/".$keTxt[1];
                    $inArr['ux_xiaoke_txt'] = json_encode($arr1, true);
                    //ddd($inArr);
                    $ux_id = Db::name('ditui_user_xiaoke')->insertGetId($inArr);
                    $inArr['ux_id'] = $ux_id;
                    $perXKobj = $inArr;
                }
                $perXKobj['keName'] = $keTxt[0];
                $inArr['ux_xiaoke_arr'] = json_decode($perXKobj['ux_xiaoke_txt'], true);

                $list = $this->getPerXueKC($dl_id);
                $back['key'] = "add";
                $back['addObj'] = $perXKobj;
                $back['list'] = $list;
                return $this->renderSuccess('添加成功', '', $back);
            }
            //删除学员所添加的课程
            if($opm=='del'){
                $back['key'] = "del";

                $sql = "delete from ".getTable('ditui_user_xiaoke')." where ux_dl_id=$dl_id and ux_ket_id=$keId and ux_shopid=".$this->ad_shopid;
                $back['delObj'] = Db::query($sql);

                $list = $this->getPerXueKC($dl_id);
                $back['list'] = $list;
                return $this->renderSuccess('删除成功', '', $back);
            }
        }
        else{
            return $this->renderError('请选择要添加的课程');
        }
    }

    /** 运行消课函数，执行真正的消课 */
    public function runSetXK(){
        $paramArr = $this->request->param();
        $ux_id = isset($paramArr['ux_id']) ? $paramArr['ux_id'] : 0;
        $yuanIndex = isset($paramArr['yuanIndex']) ? $paramArr['yuanIndex'] : -1;
        $setObj = isset($paramArr['setObj']) ? $paramArr['setObj'] : null;
        if($ux_id>0 && $yuanIndex>-1 && $setObj!=null){
            $xiaokeObj = Db::name('ditui_user_xiaoke')->where('ux_id', $ux_id)->find();
            $xiaokeArr = json_decode($xiaokeObj['ux_xiaoke_txt'], true);
            $xiaokeArr[$yuanIndex] = $setObj;
            $newXkStr = json_encode($xiaokeArr, true);
            //计算已消耗的课程
            $yesKe = 0;
            foreach($xiaokeArr as $kk => $vv){
                if($vv['k_type']=='10' || $vv['k_type']=='40' || $vv['k_type']=='50'){
                    $yesKe += intval($vv['k_use']);
                }
            }
            $xiaokeSSS = $yesKe.'/'.count($xiaokeArr);
            Db::name('ditui_user_xiaoke')->where('ux_id',$ux_id)->update(['ux_xiaoke_txt'=>$newXkStr, 'ux_xiaoke_jd'=>$xiaokeSSS]);
            $back['xiaoke_jd'] = $xiaokeSSS;
            $back['xiaokeObj'] = $xiaokeObj;
            return $this->renderSuccess('设置成功', '', $back);
        }
        else{
            return $this->renderError('消课设置参数错误');
        }
    }

    /**获取每个学员所归属的课程*/
    private function getPerXueKC($dl_id){
        $sql = "select * from ".getTable('ditui_user_xiaoke')." where ux_dl_id=".$dl_id." order by ux_ket_id desc";
        $list = Db::query($sql);
        $zdbigArr = $this->getKeZd();
        foreach($list as $key => $vvv){
            $keId = $vvv['ux_ket_id'];
            $keObj = $zdbigArr[$keId-1];
            $keTxt = explode('#', $keObj);
            $list[$key]['keName'] = $keTxt[0];
            $list[$key]['ux_xiaoke_arr'] = json_decode($vvv['ux_xiaoke_txt'], true);
        }
        return  $list;
    }

    /**获取课程字典信息*/
    private function getKeZd($keId=0){
        $zidianObj = Db::name('ditui_zidian')->where('dz_shopid', $this->ad_shopid)->find();
        if(!$zidianObj){
            $zidianCC = new Dtzidian();
            $keArr = $zidianCC->getArr(4);
        }
        else{
            $keArr = explode(',', $zidianObj['dz_kkccstr']);
        }

        $backObj = null;
        if($keId>0){
            foreach ($keArr as $kk => $vv){
                if($kk+1 == $keId){
                    $backObj = $vv;
                    break;
                }
            }
        }

        return $backObj==null ? $keArr : $backObj;
    }
}
